<?php

    class Zend_View_Helper_Event extends Zend_View_Helper_Partial
    {

        private $messageModel, $commonModel, $user, //user who created event
                $friendGroupsAndFriends, $event_id, $customModel;

        function init($event_id, $type = 'both')
        { // type both will give popup for both attending and waiting
            $this->event_id = $event_id;
            $this->customModel = $custom_model = new Application_Model_Custom();
            $this->commonModel = new Application_Model_Common();
            
            
            // get event creator id
            $this->user = $this->getEventCreator($event_id);
            
            //get event members
            $user_type = "attending";
            $attendingUsers = $this->getEventMembers($event_id, $user_type);
            $user_type = "waiting";
            $waitingUsers = $this->getEventMembers($event_id, $user_type);


            //get friend list
            //$friend_group_records = $custom_model->getFriendsAndGroups($this->user);

            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();


            $friend_group_records = "";
            if (!empty($y))
                $friend_group_records = $this->getFriendGroupAndFriends($x->read()->user_id);

            //prd($friend_group_records);
            //attending users records in an array
            $data = array();
            foreach ($attendingUsers as $attKey => $attVal)
            {
                $data[$attKey]['user'] = $attVal;
                
                $data[$attKey]['frndArr'] = $this->getFriendList($y->user_id);
                $data[$attKey]['isLoggedIn'] = !empty($y) ? 'yes' : 'no';
                $data[$attKey]['friendGroupsAndFriends'] = $friend_group_records;
                if (!empty($y))
                    $data[$attKey]['myEvents'] = $this->commonModel->getAllRecordDetail(TABLE_EVENTS, "event_ended = '0' AND event_created_by = " . $x->read()->user_id, "event_id DESC");
                $data[$attKey]['friend_created_events'] = $this->getCreatedEvent($attVal['user_id']);
                $data[$attKey]['getUserEventsCounts'] = $custom_model->getUserEventsCounts($attVal['user_id']);
            }

            //prd($data);

            if ($type == 'both')
            {

                //waiting users record in an array    
                if (!empty($waitingUsers))
                    foreach ($waitingUsers as $waitKey => $waitVal)
                    {
                        $data[$waitKey]['user'] = $waitVal;
                        
                        $data[$waitKey]['frndArr'] = $this->getFriendList($waitVal['user_id']);
                        $data[$waitKey]['isLoggedIn'] = !empty($y) ? 'yes' : 'no';
                        $data[$waitKey]['friendGroupsAndFriends'] = $friend_group_records;
                        if (!empty($y))
                            $data[$waitKey]['myEvents'] = $this->commonModel->getAllRecordDetail(TABLE_EVENTS, "event_ended = '0' AND event_created_by = " . $x->read()->user_id, "event_id DESC");
                        $data[$waitKey]['friend_created_events'] = $this->getCreatedEvent($waitVal['user_id']);
                        $data[$waitKey]['getUserEventsCounts'] = $custom_model->getUserEventsCounts($waitVal['user_id']);
                    }
            }

            //prd($data);
            $this->eventArr = $data;

            echo $this->view->partialLoop('eventpopup.phtml', $this->eventArr);
        }

        function getChatPopup($userArr, $echo = true)
        { // type both will give popup for both attending and waiting
            $this->customModel = $custom_model = new Application_Model_Custom();
            $this->commonModel = new Application_Model_Common();

            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();


            $friend_group_records = "";
            if (!empty($y))
                $friend_group_records = $this->getFriendGroupAndFriends($y->user_id);

            $data = array();
            foreach ($userArr as $uKey => $uVal)
            {
                $data[$uKey]['user'] = $this->commonModel->getdata(TABLE_USERS, 'user_id=' . $uVal);
                $data[$uKey]['frndArr'] = $this->getFriendList($y->user_id);
                $data[$uKey]['isLoggedIn'] = !empty($y) ? 'yes' : 'no';
                $data[$uKey]['friendGroupsAndFriends'] = $friend_group_records;
                if (!empty($y))
                    $data[$uKey]['myEvents'] = $this->commonModel->getAllRecordDetail(TABLE_EVENTS, "event_ended = '0' AND event_created_by = " . $x->read()->user_id, "event_id DESC");
                $data[$uKey]['friend_created_events'] = $this->getCreatedEvent($uVal);
                $data[$uKey]['getUserEventsCounts'] = $custom_model->getUserEventsCounts($uVal);
                $data[$uKey]['key'] = $uKey;
            }

            
            $this->chatArr = $data;
            
            if ($echo)
                echo $this->view->partialLoop('eventpopup.phtml', $this->chatArr);
            else
                return $this->view->partialLoop('eventpopup.phtml', $this->chatArr);
        }

        function message($type = 'inbox')
        {

            $this->customModel = $custom_model = new Application_Model_Custom();
            $this->commonModel = new Application_Model_Common();

            $x = new Zend_Auth_Storage_Session('User_Auth');
            $y = $x->read();


            $this->user_id = $y->user_id;

            //get message users

            $messageUsers = $this->getMessageMembers($this->user_id, $type);

            $friend_group_records = "";
            if (!empty($y))
                $friend_group_records = $this->getFriendGroupAndFriends($x->read()->user_id);

            //prd($friend_group_records);
            //attending users records in an array
            $data = array();
            foreach ($messageUsers as $attKey => $attVal)
            {
                $data[$attKey]['user'] = $attVal;
                $data[$attKey]['key'] = $attKey;
                $data[$attKey]['pmessage'] = $type;
                $data[$attKey]['frndArr'] = $this->getFriendList($y->user_id);
                $data[$attKey]['isLoggedIn'] = !empty($y) ? 'yes' : 'no';
                $data[$attKey]['friendGroupsAndFriends'] = $friend_group_records;
                if (!empty($y))
                    $data[$attKey]['myEvents'] = $this->commonModel->getAllRecordDetail(TABLE_EVENTS, "event_ended = '0' AND event_created_by = " . $x->read()->user_id, "event_id DESC");
                $data[$attKey]['friend_created_events'] = $this->getCreatedEvent($attVal['user_id']);
                $data[$attKey]['getUserEventsCounts'] = $custom_model->getUserEventsCounts($attVal['user_id']);
            }
            
            
            $this->messageArr = $data;
            
            echo $this->view->partialLoop('eventpopup.phtml', $this->messageArr);
        }

        function getCreatedEvent($user_id)
        {

            $custom_model = new Application_Model_Custom();
//            $records = $custom_model->getAllEventDetails("event_created_by = $user_id AND event_ended = '0'", "event_id DESC");
            $records = $custom_model->getAllEventDetails("event_created_by = $user_id", "event_id DESC");
//            prd($records);
            return $records;
        }

        function getFriendGroupAndFriends($user_id, $showOnlyOnce = false)
        {
            $friend_group_records = $this->customModel->getFriendsAndGroups($user_id);

            $new_test_array = array();
            foreach ($friend_group_records as $fr_key => $fr_value)
            {
                foreach ($fr_value["friends_records"] as $new_key => $new_value)
                {
                    $friendEventsCounts = $this->customModel->getUserEventsCounts($new_value["user_id"]);

                    //=============================//
                    if ($showOnlyOnce == TRUE)
                    {
                        if (!in_array($new_value["user_id"], $new_test_array))
                        {
                            $new_test_array[] = $new_value["user_id"];
                            foreach ($friendEventsCounts as $fe_key => $fe_value)
                            {
                                $friend_group_records[$fr_key]["friends_records"][$new_key][$fe_key] = $fe_value;
                            }
                        }
                        else
                        {
                            unset($friend_group_records[$fr_key]["friends_records"][$new_key]);
                        }
                    }
                    else
                    {
                        $new_test_array[] = $new_value["user_id"];
                        foreach ($friendEventsCounts as $fe_key => $fe_value)
                        {
                            $friend_group_records[$fr_key]["friends_records"][$new_key][$fe_key] = $fe_value;
                        }
                    }

                    //---------------------------//
                }
            }
            return $friend_group_records;
        }

        function getFriendList($user_id)
        {
            if (!empty($user_id))
                $friend_records = $this->commonModel->getAllRecordFromInnerJoin('friend_requests', TABLE_USERS, TABLE_USERS . '.user_id = friend_requests.user_sent_to', 'user_sent_from=' . $user_id . ' and user_active="1"');
            $frndArr = array();
            if (!empty($friend_records))
            {
                foreach ($friend_records as $fKey => $fVal)
                {
                    $frndArr[] = $fVal['user_id'];
                }
            }
//            prd($frndArr);
            return $frndArr;
        }

        function getMessageMembers($user_id, $user_type = "inbox")
        {
            if ($user_type == "inbox")
            {
                $cond = "message_to =  $user_id and message_deleted_to = '0' ";
                $result = $this->commonModel->getAllRecordFromInnerJoin(TABLE_MESSAGES, TABLE_USERS, TABLE_USERS . ".user_id = " . TABLE_MESSAGES . ".message_from", $cond);
            }
            else
            {
                $cond = "message_from =  $user_id and message_deleted_from = '0' ";
                $result = $this->commonModel->getAllRecordFromInnerJoin(TABLE_MESSAGES, TABLE_USERS, TABLE_USERS . ".user_id = " . TABLE_MESSAGES . ".message_to", $cond);
            }


            return $result;
        }

        function getEventMembers($event_id, $user_type = "attending")
        {

            if ($user_type == "attending")
                $cond = "ej_is_accepted = '1' and ej_is_invited = '0' and ej_event_id = '$event_id'";
            else
                $cond = "ej_is_accepted = '0' and ej_is_invited = '0' and ej_event_id = '$event_id'  ";

            $result = $this->commonModel->getAllRecordFromInnerJoin(TABLE_EVENT_JOIN, TABLE_USERS, TABLE_USERS . ".user_id = " . TABLE_EVENT_JOIN . ".ej_user_id", $cond);
            //prd($result);
            return $result;
        }

        function getEventCreator($event_id)
        {

            $result = $this->commonModel->getdata(TABLE_EVENTS, "event_id = '" . $event_id . "' ");
            return $result['event_created_by'];
        }

    }

    